package com.shiguo365.market.presenter.implement;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.shiguo365.market.display.view.IDetailView;
import com.shiguo365.market.presenter.contract.IDetailPresenter;

import biz.yfsoft.api.ae.api.AEClient;
import biz.yfsoft.api.ae.api.AEParam;
import biz.yfsoft.api.ae.api.BaseAECallback;


/**
 * Created by james on 16/8/2.
 */
public class DetailPresenter implements IDetailPresenter {

    private static final String TAG = "DetailPresenter";

    private Activity activity;
    private IDetailView iDetailView;

    public DetailPresenter(@NonNull Activity activity,@NonNull IDetailView detailView){
        this.activity = activity;
        this.iDetailView = detailView;
    }

    @Override
    public void getdetail(String pid) {
        AEClient _client = new AEClient("shiguo.data.getProDetails");
        AEParam aeParam =new AEParam();

        aeParam.set("pid",pid);

        _client.call(aeParam,new BaseAECallback() {

            @Override
            public void onSuccess(JSONObject a) {
                Log.e(TAG,"onSuccess====="+a.toJSONString());
                //Log.d(TAG,a.toJSONString());
                iDetailView.onGetDetailInfoOk(a);
            }

            @Override
            public void onError( String errno, String message) {
                Log.e(TAG,"onFailure ......." + errno + ";message:" + message);
                //iRegisterView.showRegisterMessages("对不起,注册失败,失败原因:"+message);
            }

            @Override
            public void onFinally(String s) {
                Log.d(TAG,"onFinally,,,,,");
            }
        });
    }

    @Override
    public void addDetailCart(int uid,String pid) {
        AEClient _client = new AEClient("shiguo.cart.addCart");
        AEParam aeParam =new AEParam();

        aeParam.set("pid",pid);
        aeParam.set("uid",uid);
        aeParam.set("pnum",1);

        _client.call(aeParam,new BaseAECallback() {

            @Override
            public void onSuccess(JSONObject a) {
                Log.e(TAG,"onSuccess====="+a.toJSONString());

                iDetailView.showDetailMessages("添加成功,在购物车等亲");
            }

            @Override
            public void onError( String errno, String message) {
                Log.e(TAG,"onFailure ......." + errno + ";message:" + message);
                //iRegisterView.showRegisterMessages("对不起,注册失败,失败原因:"+message);
            }

            @Override
            public void onFinally(String s) {
                Log.d(TAG,"onFinally,,,,,");
            }
        });
    }
}
